package com.spring.sperapc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mangofactory.swagger.annotations.ApiIgnore;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.User;
import com.spring.security.web.security.Contents;
import com.spring.security.web.service.RoleService;
import com.spring.sperapc.web.model.Family;
import com.spring.sperapc.web.model.FamilyMember;
import com.spring.sperapc.web.service.FamilyMemberService;
import com.spring.sperapc.web.service.UserFamilyService;

/**
 * Created by wojunyu on 2015/8/12.
 */
@Controller
@RequestMapping(value = "/userFamily")
@ApiIgnore
public class UserFamilyController {
    static Logger logger = Logger.getLogger(UserFamilyController.class.getName());

    @Resource
    private UserFamilyService userFamilyService;

    @Resource
    private FamilyMemberService familyMemberService;

    @Resource
    private RoleService roleService;

    /**
     * 跳转到家庭维护界面
     * @return
     */
    @RequestMapping(value = "/updateForm")
    public ModelAndView updateForm(HttpServletRequest request){
        Map<String, Object> retD = new HashMap<String, Object>();
        try{
            User u = (User)request.getSession().getAttribute(Contents.USER_INFO);
            if(!ApplicationUtils.isNotNull(u.getId())){
                retD.put("msg","用户未登录,请先登录");
                return new ModelAndView("login",retD);
            }

            Map<String,Object> m = new HashMap<String,Object>();
            m.put("c_user_id",u.getId());
            //m.put("c_family_id","3b629178-a21b-4bd0-a53f-b4249a29bf7f");
            Family family = userFamilyService.selectByPrimaryKey(m);
            retD.put("dataSet",family);
        }catch (Exception e){
            e.printStackTrace();
        }

        return new ModelAndView("user/family_update",retD);
    }
    
//    @RequestMapping(value = "/showFamilyMem")
//    public ModelAndView showFamilyMem(HttpServletRequest request){
//        Map<String, Object> retD = new HashMap<String, Object>();
//        try{
//            User u = (User)request.getSession().getAttribute(Contents.USER_INFO);
//            if(!ApplicationUtils.isNotNull(u.getId())){
//                retD.put("msg","用户未登录,请先登录");
//                return new ModelAndView("login",retD);
//            }
//
//            Map m = new HashMap();
//            m.put("c_user_id",u.getId());
//            FamilyMember familyMember = familyMemberService.selectByPrimaryKey(m);
//            if(!ApplicationUtils.isNotNull(familyMember.getcFamilyId())){
//                retD.put("msg","完善家庭信息，请先完善家庭信息");
//                return new ModelAndView("index",retD);
//            }
//            retD.put("c_family_id", familyMember.getcFamilyId());
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//        return new ModelAndView("oa/task",retD);
//    }

    /**
     * 保存家庭信息
     * @param family
     * @return
     */
    @RequestMapping(value = "/saveUserFamily")
    public ModelAndView saveUserDetail(Family family,HttpServletRequest request){
        User u = (User)request.getSession().getAttribute(Contents.USER_INFO);
        //family.setcUserId(u.getId());
        String ret = "";
        Map<String, Object> retD = new HashMap<String, Object>();
        int iss = 0;
        if(ApplicationUtils.isNotNull(family.getId())){
            iss = userFamilyService.updateByPrimaryKeySelective(family);
        }else{
            family.setId(ApplicationUtils.randomUUID());
            iss = userFamilyService.insertSelective(family,u);
        }

        if(iss==0){
            ret = "保存失败";
        }else{
            ret = "保存成功";
        }
        retD.put("status", iss+"");
        retD.put("msg", ret);
        Map<String,Object> m = new HashMap<String,Object>();
        m.put("c_family_id",family.getId());
        Family queryFamily = userFamilyService.selectByPrimaryKey(m);
        retD.put("dataSet",queryFamily);
        return new ModelAndView("user/family_show",retD);
    }

    /**
     * 跳转到成员新增界面
     * @return
     */
//    @RequestMapping(value = "/updateMemberForm")
//    public ModelAndView insertMemberForm(String c_family_member_id,HttpServletRequest request){
//        Map<String, Object> retD = new HashMap<String, Object>();
//        try{
//            User u = (User)request.getSession().getAttribute(Contents.USER_INFO);
//            if(!ApplicationUtils.isNotNull(u.getId())){
//                retD.put("msg","用户未登录,请先登录");
//                return new ModelAndView("login",retD);
//            }
//
//            Map m = new HashMap();
//            m.put("c_user_id",u.getId());
//            //成员主键存在时，是成员的更新操作，否则是成员的新增操作
//            m.put("id",c_family_member_id);
//            FamilyMember familyMember = familyMemberService.selectByPrimaryKey(m);
//            if(!ApplicationUtils.isNotNull(familyMember.getcFamilyId())){
//                retD.put("msg","完善家庭信息，请先完善家庭信息");
//                return new ModelAndView("index",retD);
//            }
//            FamilyMember fam = new FamilyMember();
//            if(ApplicationUtils.isNotNull(c_family_member_id)){
//                fam = familyMember;
//            }else{
//                fam.setcFamilyId(familyMember.getcFamilyId());
//            }
//            retD.put("dataSet",fam);
//            retD.put("rolelist",roleService.selectRoles(null));
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//        return new ModelAndView("user/familymem_update",retD);
//    }

    /**
     * 保存家庭成员信息
     * @param familyMember
     * @return
     */
    @RequestMapping(value = "/saveMember")
    public ModelAndView saveMember(FamilyMember familyMember,HttpServletRequest request){
//        User u = (User)request.getSession().getAttribute(Contents.USER_INFO);
        //family.setcUserId(u.getId());
        String ret = "";
        Map<String, Object> retD = new HashMap<String, Object>();
        int iss = 0;
        if(ApplicationUtils.isNotNull(familyMember.getcFamilyId())){
            iss = familyMemberService.updateByPrimaryKeySelective(familyMember);
        }else{
            familyMember.setcFamilyId(ApplicationUtils.randomUUID());
            iss = familyMemberService.insertSelective(familyMember);
        }

        if(iss==0){
            ret = "保存失败";
        }else{
            ret = "保存成功";
        }
        retD.put("status", iss+"");
        retD.put("msg", ret);
        Map<String,Object> m = new HashMap<String,Object>();
        m.put("c_family_id",familyMember.getcFamilyId());
        List<FamilyMember> familyMemList = familyMemberService.selectByFamilyId(m);
        retD.put("c_family_id",familyMember.getcFamilyId());
        retD.put("dataSet",familyMemList);
        retD.put("rolelist",roleService.selectRoles(null));
        return new ModelAndView("user/familymem_show",retD);
    }


    /**
     * 查询家庭成员信息
     * @param c_family_id
     * @return
     */
    @RequestMapping(value = "/queryFamilyMember")
    @ResponseBody
    public Object queryFamilyMember(String c_family_id,String page,String pageSize,HttpServletRequest request){
        System.out.println("------->"+page+","+pageSize);
//        User u = (User)request.getSession().getAttribute(Contents.USER_INFO);
        Map<String,Object> m = new HashMap<String,Object>();
        m.put("c_family_id",c_family_id);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
        List<FamilyMember> familyMemList = familyMemberService.selectByFamilyId(m);
        PageInfo pageInfo = new PageInfo(familyMemList);
        Map<String,Object> retD = new HashMap<String,Object>();
        retD.put("data",familyMemList);
        retD.put("total",pageInfo.getTotal());
        return retD;
    }
}
